package com.monolithiot.cdn.management.service.impl;

import com.monolithiot.cdn.management.common.entity.User;
import com.monolithiot.cdn.management.common.util.MD5Util;
import com.monolithiot.cdn.management.dao.BaseMapper;
import com.monolithiot.cdn.management.dao.mapper.UserMapper;
import com.monolithiot.cdn.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 2018/11/18 Create by 郭文梁
 * UserServiceImpl
 * 用户业务服务实现
 *
 * @author 郭文梁
 * @date 2018/11/18
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    BaseMapper<User> getMapper() {
        return userMapper;
    }

    @Override
    public User register(User user) {
        user.setPassword(MD5Util.md5(user.getPassword()));
        int res = userMapper.insert(user);
        return res > 0 ? user : null;
    }

    @Override
    public User login(User user) {
        User query = new User();
        query.setName(user.getName());
        User res = userMapper.selectOne(query);
        if (res == null) {
            return null;
        }

        String savedPassword = res.getPassword();
        String password = MD5Util.md5(user.getPassword());

        if (savedPassword.equalsIgnoreCase(password)) {
            return res;
        } else {
            return null;
        }
    }
}
